package com.sailfish.springbootdemo.dao.db7;

import com.sailfish.springbootdemo.common.DB7TableName;
import com.sailfish.springbootdemo.pojo.db7.TbTaskVersion;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface TbTaskVersionDao {

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TASK_VERSION +
            " where 1=1" +
            "<if test='task_type != null and task_type!=\"\"'>" +
            " and task_type = #{task_type}" +
            "</if>" +
            " order by sort desc limit 1" +
            "</script>")
    List<TbTaskVersion> getNewestTaskVersion(@Param("task_type") String task_type);

    @Select("<script> " +
            "select \n" +
            "flow_no" +
            " from " + DB7TableName.TB_TASK_VERSION +
            " where 1=1" +
            "<if test='type != null and type!=\"\"'>" +
            " and task_type = #{type}" +
            "</if>" +
            " and status = 3" +
            "</script>")
    List<String> getArchivedFlows(@Param("type") String type);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TASK_VERSION +
            " where 1=1" +
            "<if test='type != null and type!=\"\"'>" +
            " and task_type = #{type}" +
            "</if>" +
            " and status = 3" +
            "</script>")
    List<TbTaskVersion> getArchievdTasks(@Param("type") String type);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TASK_VERSION +
            " where 1=1 and status!=5 " +
            "<if test='comp_code != null and comp_code!=\"\"'>" +
            " and comp_code = #{comp_code}" +
            "</if>" +
            " order by sort desc limit 1" +
            "</script>")
    List<TbTaskVersion> getTaskVersionByCompCode(@Param("comp_code") String comp_code);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TASK_VERSION +
            " where 1=1 and status = 3" +
            " and (task_type = 'device' or task_type = 'vpl')" +
            "</script>")
    List<TbTaskVersion> getTaskVersionOfVplOrDevice();

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TASK_VERSION +
            " where 1=1 and status = 3" +
            "<if test='taskType != null and taskType!=\"\"'>" +
            " and task_type = #{taskType}" +
            "</if>" +
            "</script>")
    List<TbTaskVersion> getTaskVersionOfTaskType(@Param("taskType")String taskType);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TASK_VERSION +
            " where 1=1" +
            "<if test='comp_code != null and comp_code!=\"\"'>" +
            " and comp_code = #{comp_code}" +
            "</if>" +
            "<if test='task_type != null and task_type!=\"\"'>" +
            " and task_type = #{task_type}" +
            "</if>" +
            "<if test='status != null and status!=\"\"'>" +
            " and status != #{status}" +
            "</if>" +
            " order by sort desc limit 1" +
            "</script>")
    List<TbTaskVersion> getTaskVersionByCompCode2(@Param("comp_code") String comp_code,
                                                  @Param("task_type") String task_type,
                                                  @Param("status") Integer status);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TASK_VERSION +
            " where 1=1" +
            "<if test='comp_code != null and comp_code!=\"\"'>" +
            " and comp_code = #{comp_code}" +
            "</if>" +
            "<if test='task_type != null and task_type!=\"\"'>" +
            " and task_type = #{task_type}" +
            "</if>" +
            "<if test='status != null and status!=\"\"'>" +
            " and status = #{status}" +
            "</if>" +
            " order by sort desc limit 1" +
            "</script>")
    List<TbTaskVersion> getTaskVersionByCompCode3(@Param("comp_code") String comp_code,
                                                  @Param("task_type") String task_type,
                                                  @Param("status") Integer status);

    @Select("<script> " +
            "select \n" +
            "*" +
            " from " + DB7TableName.TB_TASK_VERSION +
            " where 1=1" +
            "<if test='comp_code != null and comp_code!=\"\"'>" +
            " and comp_code = #{comp_code}" +
            "</if>" +
            "<if test='task_type != null and task_type!=\"\"'>" +
            " and task_type = #{task_type}" +
            "</if>" +
            " order by sort desc limit 1" +
            "</script>")
    List<TbTaskVersion> getTaskVersionByCompCode4(@Param("comp_code") String comp_code,
                                                  @Param("task_type") String task_type);


    @Insert("<script>" +
            "insert into " + DB7TableName.TB_TASK_VERSION + " " +
            "(task_type, version, create_time, create_user, update_time, update_user, status, sort,flow_no,comp_code) "+
            "values "+
            "(#{task_type}, #{version}, #{create_time}, #{create_user}, #{update_time}, #{update_user}, #{status}, #{sort},#{flow_no},#{comp_code})"+
    "</script>")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer insertTaskVersion(TbTaskVersion tbTaskVersion);


    @Update("<script> " +
            "update " + DB7TableName.TB_TASK_VERSION +
            " set " +
            "status = #{status} " +
            "where flow_no = #{flow_no} " +
            "</script>"
    )
    Integer updateTaskVersionStatus(TbTaskVersion tbTaskVersion);

    @Delete("<script>" +
            "delete from " + DB7TableName.TB_TASK_VERSION + " " +
            "where flow_no = #{flow_no}" +
            "</script>")
    Integer deleteTaskVersionByFlowNo(@Param("flow_no") String flow_no);

    @Select("<script>" +
            "select * from " + DB7TableName.TB_TASK_VERSION + " " +
            "where flow_no = #{flow_no}" +
            "order by id desc limit 1" +
            "</script>")
    List<TbTaskVersion> getTaskVersionByFlowNo(@Param("flow_no") String flow_no);

    @Select("<script>" +
            "SELECT * FROM " + DB7TableName.TB_TASK_VERSION + " " +
            "<if test='flowNos != null and !flowNos.isEmpty()'> " +
            "WHERE flow_no IN " +
            "<foreach item='item' collection='flowNos' open='(' separator=',' close=')'>" +
            "#{item} " +
            "</foreach>" +
            "</if>" +
            "ORDER BY id DESC" +
            "</script>")
    List<TbTaskVersion> getTaskVersionByFlowNoList(@Param("flowNos") List<String> flowNos);

    @Update("<script> " +
            "update " + DB7TableName.TB_TASK_VERSION +
            " set " +
            "status = 5 " +
            "where flow_no = #{flow_no} " +
            "</script>"
    )
    Integer cancelTaskVersion(@Param("flow_no") String flow_no);

    @Select("<script>" +
            "select * from " + DB7TableName.TB_TASK_VERSION + " " +
            "where comp_code IS NOT NULL" +
            "</script>")
    List<TbTaskVersion> getAllTaskVersion();
}
